const InitADObserver = require('bindings')('AD-Observer');
const fork = require('child_process').fork;
const path = require('path')

class ADObServer {
    monitor;
    delCheck;
    args;
    changeCheck;
    
    constructor(args) {
        this.args = args
        // this.args = {
        //     hostName: 'localhost',
        //     port: '389',
        //     serverUrl: 'ldap://localhost:389',
        //     bindDN: 'cn=administrator,cn=users,dc=test,dc=authing,dc=com',
        //     baseDN: 'CN=steamory,CN=users,DC=test,DC=authing,DC=com',
        //     password: 'Authing.cn200500404',
        //     changedAttribs: ['name', 'isDeleted', 'uSNChanged'],
        //     // changedAttribs: 'name',
        //     filterStr: 'ObjectClass=*',
        //   }
        // this.changeCheck = initChangeCheck(args)
    }
    
    // open() {
    //     // this.changeCheck.send("open")
    // }

    // bind() {
    //     // this.changeCheck.send("bind")
    // }
    initMonitor(monitor) {
        this.monitor = monitor
        // initDelCheck(this.args, monitor)
        initChangeCheck(this.args, monitor)
    }
    startMonitor() {
        // this.changeCheck.send("startMonitor")
    }
}

function initDelCheck(args, monitor) {
    const child = fork(path.join(__dirname + '/addOrDelMonitor.js'), [JSON.stringify(args)]);
    child.on('message', function (data) {
        monitor.emit('del', data);
    })
    
} 

function initChangeCheck(args, monitor) {
    const child = fork(path.join(__dirname + '/changeCheck.js'), [JSON.stringify(args)]);
    child.on('message', function (data) {
        monitor.emit('change', data);
    })
} 

module.exports = ADObServer
